# -*- coding: utf-8 -*-

import hashlib
from base import UserCase, Runner, TestSuite


class Case(UserCase):

    def test_rule_list(self):
        res = self.c.open('/api/rule/list')
        self.assertEqual(res['code'], 0)

    def test_rule_create(self):
        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)

        id = res['data']['id']

        res = self.c.open('/api/rule/list', {'orderBy': 'rule.id', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['itemList'][0]['name'], p['name'])

        res = self.c.open('/api/rule/read', {'id': id})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['name'], p['name'])


    def test_rule_delete(self):
        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/rule/list', {'orderBy': 'rule.id', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['itemList'][0]['name'], p['name'])

        res = self.c.open('/api/rule/delete', {'id': id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/rule/list')
        self.assertEqual(res['code'], 0)
        self.assertNotEqual(res['data']['itemList'][0]['id'], id)


    def test_rule_update(self):
        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/rule/read', {'id': id})
        self.assertEqual(res['data']['name'], p['name'])

        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        p['id'] = id
        res = self.c.open('/api/rule/update', p)
        self.assertEqual(res['code'], 0)
        res = self.c.open('/api/rule/read', {'id': id})
        self.assertEqual(res['data']['name'], p['name'])
        self.assertEqual(res['data']['code'], p['code'])
        self.assertEqual(res['data']['field'], p['field'])
        self.assertEqual(res['data']['op'], p['op'])
        self.assertEqual(res['data']['value'], p['value'])


    def test_rule_option(self):
        res = self.c.open('/api/rule/option')
        self.assertEqual(res['code'], 0)

        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/rule/option')
        self.assertEqual(res['code'], 0)


    def test_rule_option_filter(self):
        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        pp = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', pp)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/rule/option', {'q': p['name']})
        self.assertEqual(res['data']['itemList'][0]['value'], id)
        self.assertEqual(res['data']['itemList'][0]['name'], p['name'])



test_list = [
    'test_rule_list',
    'test_rule_create',
    'test_rule_delete',
    'test_rule_update',
    'test_rule_option',
    'test_rule_option_filter',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



